package com.retail.member.client.impl;

import cn.hutool.core.util.ObjectUtil;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.core.entity.result.Result;
import com.retail.member.client.ShopClient;
import com.retail.member.entity.vo.RetailMultiShopResVo;
import com.retail.member.entity.vo.RetailShopResVo;
import com.retail.order.entity.vo.RetailSettleOrderVo;
import com.retail.order.service.RetailSettleOrderService;
import com.retail.shop.entity.request.RetailAdminShopRequest;
import com.retail.shop.entity.vo.RetailShopVo;
import com.retail.shop.enums.ExamineVerifyEnums;
import com.retail.shop.service.RetailAdminShopService;
import com.retail.shop.service.RetailShopService;
import com.retail.system.entity.vo.AdminVo;
import com.retail.system.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShopClientImpl implements ShopClient {

    @Autowired
    private RetailShopService retailShopService;

    @Autowired
    private RetailAdminShopService retailAdminShopService;

    @Autowired
    private AdminService adminService;


    @Override
    public Result<List<RetailShopVo>> findOwnerShopByAdminId(String adminId) {
        List<RetailShopVo> rets = retailShopService.findOwnerShopByAdminId(adminId);
        return Result.success(rets);
    }

    @Override
    public Result<List<RetailMultiShopResVo>> shopList(Long adminId) {
        RetailMultiShopResVo retailMultiShopResVo = new RetailMultiShopResVo();
        List<RetailShopVo> myshops = retailShopService.findOwnerShopByAdminId(adminId + "");
        retailMultiShopResVo.setMyShops(myshops);
        List<RetailShopVo> groupShops = new ArrayList<>();
        List<AdminVo> level1ChildAdmins = adminService.childAdminByParentId(adminId);
        level1ChildAdmins.forEach(admin -> {
            groupShops.addAll(retailShopService.findOwnerShopByAdminId(admin.getId() + ""));
            List<AdminVo> level2ChildAdmins = adminService.childAdminByParentId(admin.getId());
            level2ChildAdmins.forEach(lad -> {
                groupShops.addAll(retailShopService.findOwnerShopByAdminId(lad.getId() + ""));
            });
        });
        retailMultiShopResVo.setGroupShops(groupShops);
        List<RetailShopVo> all = new ArrayList<>();
        all.addAll(myshops);
        all.addAll(groupShops);
        retailMultiShopResVo.setAllShops(all);
        return Result.success(retailMultiShopResVo);
    }

    @Override
    public Result<List<RetailShopVo>> getOfflineAllShopList(String search, String city) {
        List<RetailShopVo> myshops = retailShopService.getOfflineAllShopList(search, city);
        return Result.success(myshops);
    }

    @Autowired
    private RetailSettleOrderService orderService;

    @Override
    public Result<RetailShopResVo> shopDetail(Long shopId) {
        RetailShopResVo res = new RetailShopResVo();
        RetailShopVo shop = retailShopService.getModel(shopId);
        res.setShop(shop);
        if (ObjectUtil.isNotEmpty(shop.getPoiId())) {
            List<RetailSettleOrderVo> orders = orderService.getOrdersByShop(shop.getPoiId());
            if (ObjectUtil.isNotEmpty(orders)) {
                res.setOrders(orders);
            } else {
                res.setOrders(null);
            }
        } else {
            res.setOrders(null);
        }
        if (ObjectUtil.isNotEmpty(shop.getPoiId())) {
            AdminVo adminVo = adminService.getModelByPoiId(shop.getPoiId());
            if (ObjectUtil.isNotEmpty(adminVo)) {
                res.setAdmin(adminVo);
            } else {
                res.setAdmin(null);
            }
        } else {
            res.setAdmin(null);
        }

        return Result.success(res);
    }

    @Override
    public Result<List<RetailShopVo>> myShopList(Long adminId, String search) {
        List<RetailShopVo> myshops = retailShopService.myShopList(adminId + "", search);
        return Result.success(myshops);
    }

    @Override
    public Result<List<RetailShopVo>> toBeBindShop() {
        List<RetailShopVo> list = retailShopService.toBeBindShop();
        return Result.success(list);
    }

    @Override
    public Result<Integer> bindShop(Long adminId, Long shopId) {

        RetailShopVo retailShopVo = retailShopService.findShopByShopId(shopId);
        if (ObjectUtil.isNotEmpty(retailShopVo)) {
            return Result.fail("商户已被绑定，请联系管理员！");
        }


        RetailAdminShopRequest adminShopRequest = new RetailAdminShopRequest();
        adminShopRequest.setAdminId(adminId);
        adminShopRequest.setShopId(shopId);
        adminShopRequest.setExamineVerify(ExamineVerifyEnums.UN_EXAMINE_VERIFY.getValue());

        adminShopRequest.setDelFlag(DelFlagEnum.NO.getValue());
        adminShopRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
        adminShopRequest.setCreateUser(1L);
        adminShopRequest.setRemark("");

        Long id = retailAdminShopService.save(adminShopRequest);
        return Result.success(id);
    }

}
